"""
窗口几个按钮功能实现
"""
import os
import subprocess
import winreg

import win32com.client

from config.conf import Version
from plugs.log import get_time_log_path


def open_log_path():
    """打开存放日志的文件夹"""
    today_log_name = get_time_log_path()
    subprocess.Popen(["notepad.exe", today_log_name])


def force_exit():
    """强制退出，退出时关闭代理"""
    Proxy().turn_off()
    os._exit(0)


class Proxy:
    def __init__(self):
        self.registry_path = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings'

    @property
    def status(self) -> bool:
        """获取此时代理状态，开启返回True,关闭返回False"""
        key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, self.registry_path, 0, winreg.KEY_ALL_ACCESS)
        proxy_enable_value, _ = winreg.QueryValueEx(key, 'ProxyEnable')
        winreg.CloseKey(key)

        if proxy_enable_value == 1:
            return True
        else:
            return False

    def turn_on(self):
        """打开代理"""
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.registry_path, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 1)
        winreg.CloseKey(key)

    def turn_off(self):
        """关闭代理"""
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.registry_path, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 0)
        winreg.CloseKey(key)

    def set_proxy(self, proxy_server: str, exclude_addresses: list = None):
        """
        设置代理，将本地浏览器的请求发送到指定的代理服务器，默认为：127.0.0.1:7777
        :param proxy_server: 代理的host和端口，127.0.0.1:7777
        :param exclude_addresses: 不会对以下开头的进行代理，
        """
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.registry_path, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, proxy_server)
        if exclude_addresses is not None:
            winreg.SetValueEx(key, "ProxyOverride", 0, winreg.REG_SZ, ";".join(exclude_addresses))
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 1)
        winreg.CloseKey(key)


class AutoBoot:
    """开机自启动"""

    def __init__(self):
        self.lnk_name = f"{Version().app_name}.lnk"
        self.boot_path = "C:/Users/{}/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup".format(os.environ['USERNAME'])

    @property
    def status(self):
        """显示是否开启了开机自启动功能"""
        shortcut_path = os.path.join(self.boot_path, self.lnk_name)
        if os.path.exists(shortcut_path):
            return True
        return False

    def set_boot(self, enabled: bool):
        """
        设置开机自动启动: 找到当前exe的路径，创建一个快捷方式保存在startup文件夹下。
        :param enabled: True为开启，False为关闭
        """
        exe_path = os.getcwd()
        shortcut_path = os.path.join(self.boot_path, self.lnk_name)

        if enabled:
            shell = win32com.client.Dispatch("WScript.Shell")
            shortcut = shell.CreateShortCut(shortcut_path)
            shortcut.Targetpath = exe_path
            shortcut.WorkingDirectory = os.path.dirname(exe_path)
            shortcut.save()
        else:
            if os.path.exists(shortcut_path):
                os.remove(shortcut_path)


class LightCat:
    """光猫相关的操作，算了后面再写吧"""
    def __init__(self):
        self.username = "useradmin"
        self.password = "nE7jA%5m"

    def login(self):
        url = "http://192.168.1.1/cgi-bin/luci"
        data = {"username": self.username, "psd": self.password}

    def reboot(self):
        pass
